package com.neutech.solar.entity;

import com.neutech.solar.constant.Constant;
import com.neutech.solar.util.ImageUtil;

import java.awt.*;

public class Planet extends Star{
    public Star star;
    public Planet(){

    }
    public Planet(Star star,String imageKey,double AU,double e,int T,String name) {
        this.star = star;
        this.img = ImageUtil.maps.get(imageKey);
        this.width = img.getWidth(null);
        this.height = img.getHeight(null);
        this.longAxis = getLongAxis(AU);
        this.shortAxis = getShortAxis(e);
        this.x = Constant.WINDOW_WIDTH / 2 + longAxis;
        this.y = Constant.WINDOW_HEIGHT / 2;
        this.theta = 0.0;
        this.speed = getspeedbyT(T);
        this.name = name;
    }
    public Planet(Star sun, String imageName,double AU,double e,int T,String name,boolean smallPlanet) {
        this(sun,imageName,AU,e,T,name);
        this.smallPlanet = smallPlanet;
    }
    public Planet(Sun sun, String imageName,double AU,double e,int T,String name,boolean smallPlanet,boolean venus) {
        this(sun,imageName,AU,e,T,name,smallPlanet);
        this.venus = venus;

    }
    private double getspeedbyT(int t){
        return 0.1 * 365 / t;
    }

    private int getLongAxis(double AU){
        return (int)(100 * AU / 2);
    }
        private int getShortAxis(double e){
            return (int) (this.longAxis * Math.sqrt(1 - Math.pow(e,2)));
    }

    @Override
    public void draw(Graphics g) {
        Color c = g.getColor();
        g.setColor(Color.white);
        if (!smallPlanet){
            drawTrace(g);
        }

        g.drawString(name,x,y-30);

        g.setColor(c);
        g.drawImage(img,x,y,null);

        move();
    }

    private void drawTrace(Graphics g){
        int x = (int) (this.star.x - this.longAxis + star.width / 2 + Math.sqrt(Math.pow(longAxis,2)-Math.pow(shortAxis,2)));
        int y = this.star.y - this.shortAxis + star.height / 2;
        int width = 2 * this.longAxis;
        int height = 2 * this.shortAxis;
        g.drawOval(x,y,width,height);
    }



    @Override
    public void move() {
        if (!venus){
            x = (int) ((longAxis * Math.sin(theta)) + star.x + star.width / 2 - this.width / 2 + Math.sqrt(Math.pow(longAxis,2)-Math.pow(shortAxis,2)));
            y = (int) (shortAxis * Math.cos(theta)) + star.y + star.height / 2 - this.height / 2;
        }else{
            x = (int) ((longAxis * Math.cos(theta)) + star.x + star.width / 2 - this.width / 2 + Math.sqrt(Math.pow(longAxis,2)-Math.pow(shortAxis,2)));
            y = (int) (shortAxis * Math.sin(theta)) + star.y + star.height / 2 - this.height / 2;
        }
        theta += speed;
    }
}
